package com.zj.musicTools.function

import android.util.Log
import com.zj.musicTools.MainActivity.Companion.Agent
import com.zj.musicTools.function.POSNAME.*
import java.util.concurrent.ExecutorService

enum class POSNAME(val value:Int){
    NONE(0),
    INPUT(1),
    STREAM_IN(2),
    MAPPING(3),
    OUTPUT(4),
    STREAM_OUT(5),
    SIZE(6)
}

class FFMPEG(){
    var executor: ExecutorService? = null
    private lateinit var input: String
    private lateinit var output: String
    private var timeStart: String? = null
    private var timeEnd: String? = null
    private var sampling: Boolean = false
    private  var result =0
    private lateinit var commandLsit:Array<String>
    private  var callback: ((Int) -> Unit)? = null
    private var logBuf: List<String> = listOf("")
    private var logSim:Boolean = true//精简日志
    private var logErr:Boolean = false

    constructor(input:String?,output:String?,timeS:String?, timeE:String?,callback: ((Int) -> Unit)?) : this() {
        if(!input.isNullOrEmpty())this.input = input
        if(!output.isNullOrEmpty())this.output = output
        if(timeS != null)this.timeStart = timeS
        if(timeE != null)this.timeEnd = timeE
        if(callback != null)this.callback = callback
    }

    /**
     * 将一次执行的log记录下来进行，用于处理然后输出
     *
    * */
    fun logCache(line:String,err:Boolean=false){
        logBuf = logBuf.plus(listOf(if(err) "<Err>$line" else line))
        if(err)logErr = err
    }

    /**
     * ffmpeg version 6.0
     *  Copyright (c) 2000-2023 the FFmpeg developers
     *
     *   built with Android (5900059 based on r365631c) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 207d7abc1a2abf3ef8d4301736d6a7ebc224a290) (based on LLVM 9.0.8svn)
     *   configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --enable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --disable-encoder='a64multi,a64multi5,alias_pix,amv,apng,aptx,aptx_hd,asv1,asv2,avrp,avui,cinepak,cljr,comfortnoise,dpx,ffvhuff,fits,hap,ilbc_at,mlp,nellymoser,pam,pbm,pcx,pgm,pgmyuv,ppm,prores,prores_aw,prores_ks,qtrle,r10k,r210,ra_144,roq,roq_dpcm,rv10,rv20,s302m,sbc,sgi,snow,sunrast,svq1,targa,tta,utvideo,v210,v308,v408,v410,vc2,wrapped_avframe,xbm,xface,xsub,xwd,y41p,zmbv' --disable-decoders --enable-decoder='mjpeg,mpeg4,h263,h264,flv,hevc,wmv3,msmpeg4v3,msmpeg4v2,msvideo1,vc1,mpeg1video,mpeg2video,aac,ac3,ac3_fixed,m4a,amrnb,amrwb,vorbis,wmav2,truehd,tscc,tscc2,dvvideo,msrle,cinepak,indeo5,vp8,vp9,av1,mp3float,mp3,mp3adufloat,mp3adu,mp3on4float,mp3on4,aac_fixed,aac_latm,eac3,png,wmav1,wmv1,wmv2,pcm_alaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,zlib,flac,opus,mlp,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_mulaw,alac,adpcm_ms,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,adpcm_ima_qt,adpcm_ima_wav,gif' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,avi,cavsvideo,codec2,concat,dnxhd,eac3,flac,flv,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mv,ogg,png_pipe,realtext,rm,rtp,rtsp,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,sdp,srt,swf,vc1,wav,webm_dash,manifest,xmv,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,mpegvideo,mulaw,sami,srt' --cpu=armv8-a
     *   WARNING: library configuration mismatch
     *   avutil      configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --disable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --enable-encoder='apng,bmp,dvvideo,flv,gif,h263,h264,hdr,jpeg2000,ljpeg,mjpeg,mpeg1video,mpeg2video,mpeg4,msmpeg4v2,msmpeg4,png,prores,qtrle,rawvideo,tiff,wmv1,wmv2,xbm,zlib,aac,ac3,g722,g726,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,eac3,flac,mp2,opus,pcm_alaw,pcm_mulaw,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_s8,pcm_u16le,pcm_u32le,pcm_u8,sonic,truehd,tta,vorbis,wavpack,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,mov_text,srt,subrip,ttml,webvtt' --disable-decoders --enable-decoder='av1,dvvideo,flv,gif,h263,h264,hevc,mjpeg,mpeg1video,mpeg2video,mpegvideo,mpeg4,msmpeg4v2,msmpeg4v3,msvideo1,png,tscc,tscc2,vc1,vp8,vp9,webp,wmv1,wmv2,wmv3,zlib,aac,aac_latm,ac3,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,amrnb,amrwb,ape,dolby_e,eac3,flac,g722,g726,g729,m4a,mp3float,mp3,mp3adufloat,mp3adu,mp3on4,opus,pcm_alaw,pcm_mulaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,truehd,truespeech,vorbis,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,pgssub,mov_text,sami,srt,subrip,text,webvtt' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,ass,av1,avi,cavsvideo,codec2,concat,dash,dnxhd,eac3,flac,flv,g722,g726,g729,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mpegvideo,mv,mulaw,manifest,ogg,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,png_pipe,realtext,rm,rtp,rtsp,sami,sdp,srt,swf,vc1,wav,webm_dash,xmv' --cpu=armv8-a
     *   avcodec     configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --disable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --enable-encoder='apng,bmp,dvvideo,flv,gif,h263,h264,hdr,jpeg2000,ljpeg,mjpeg,mpeg1video,mpeg2video,mpeg4,msmpeg4v2,msmpeg4,png,prores,qtrle,rawvideo,tiff,wmv1,wmv2,xbm,zlib,aac,ac3,g722,g726,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,eac3,flac,mp2,opus,pcm_alaw,pcm_mulaw,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_s8,pcm_u16le,pcm_u32le,pcm_u8,sonic,truehd,tta,vorbis,wavpack,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,mov_text,srt,subrip,ttml,webvtt' --disable-decoders --enable-decoder='av1,dvvideo,flv,gif,h263,h264,hevc,mjpeg,mpeg1video,mpeg2video,mpegvideo,mpeg4,msmpeg4v2,msmpeg4v3,msvideo1,png,tscc,tscc2,vc1,vp8,vp9,webp,wmv1,wmv2,wmv3,zlib,aac,aac_latm,ac3,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,amrnb,amrwb,ape,dolby_e,eac3,flac,g722,g726,g729,m4a,mp3float,mp3,mp3adufloat,mp3adu,mp3on4,opus,pcm_alaw,pcm_mulaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,truehd,truespeech,vorbis,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,pgssub,mov_text,sami,srt,subrip,text,webvtt' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,ass,av1,avi,cavsvideo,codec2,concat,dash,dnxhd,eac3,flac,flv,g722,g726,g729,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mpegvideo,mv,mulaw,manifest,ogg,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,png_pipe,realtext,rm,rtp,rtsp,sami,sdp,srt,swf,vc1,wav,webm_dash,xmv' --cpu=armv8-a
     *   avformat    configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --disable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --enable-encoder='apng,bmp,dvvideo,flv,gif,h263,h264,hdr,jpeg2000,ljpeg,mjpeg,mpeg1video,mpeg2video,mpeg4,msmpeg4v2,msmpeg4,png,prores,qtrle,rawvideo,tiff,wmv1,wmv2,xbm,zlib,aac,ac3,g722,g726,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,eac3,flac,mp2,opus,pcm_alaw,pcm_mulaw,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_s8,pcm_u16le,pcm_u32le,pcm_u8,sonic,truehd,tta,vorbis,wavpack,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,mov_text,srt,subrip,ttml,webvtt' --disable-decoders --enable-decoder='av1,dvvideo,flv,gif,h263,h264,hevc,mjpeg,mpeg1video,mpeg2video,mpegvideo,mpeg4,msmpeg4v2,msmpeg4v3,msvideo1,png,tscc,tscc2,vc1,vp8,vp9,webp,wmv1,wmv2,wmv3,zlib,aac,aac_latm,ac3,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,amrnb,amrwb,ape,dolby_e,eac3,flac,g722,g726,g729,m4a,mp3float,mp3,mp3adufloat,mp3adu,mp3on4,opus,pcm_alaw,pcm_mulaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,truehd,truespeech,vorbis,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,pgssub,mov_text,sami,srt,subrip,text,webvtt' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,ass,av1,avi,cavsvideo,codec2,concat,dash,dnxhd,eac3,flac,flv,g722,g726,g729,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mpegvideo,mv,mulaw,manifest,ogg,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,png_pipe,realtext,rm,rtp,rtsp,sami,sdp,srt,swf,vc1,wav,webm_dash,xmv' --cpu=armv8-a
     *   avfilter    configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --disable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --enable-encoder='apng,bmp,dvvideo,flv,gif,h263,h264,hdr,jpeg2000,ljpeg,mjpeg,mpeg1video,mpeg2video,mpeg4,msmpeg4v2,msmpeg4,png,prores,qtrle,rawvideo,tiff,wmv1,wmv2,xbm,zlib,aac,ac3,g722,g726,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,eac3,flac,mp2,opus,pcm_alaw,pcm_mulaw,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_s8,pcm_u16le,pcm_u32le,pcm_u8,sonic,truehd,tta,vorbis,wavpack,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,mov_text,srt,subrip,ttml,webvtt' --disable-decoders --enable-decoder='av1,dvvideo,flv,gif,h263,h264,hevc,mjpeg,mpeg1video,mpeg2video,mpegvideo,mpeg4,msmpeg4v2,msmpeg4v3,msvideo1,png,tscc,tscc2,vc1,vp8,vp9,webp,wmv1,wmv2,wmv3,zlib,aac,aac_latm,ac3,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,amrnb,amrwb,ape,dolby_e,eac3,flac,g722,g726,g729,m4a,mp3float,mp3,mp3adufloat,mp3adu,mp3on4,opus,pcm_alaw,pcm_mulaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,truehd,truespeech,vorbis,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,pgssub,mov_text,sami,srt,subrip,text,webvtt' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,ass,av1,avi,cavsvideo,codec2,concat,dash,dnxhd,eac3,flac,flv,g722,g726,g729,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mpegvideo,mv,mulaw,manifest,ogg,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,png_pipe,realtext,rm,rtp,rtsp,sami,sdp,srt,swf,vc1,wav,webm_dash,xmv' --cpu=armv8-a
     *   swscale     configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --disable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --enable-encoder='apng,bmp,dvvideo,flv,gif,h263,h264,hdr,jpeg2000,ljpeg,mjpeg,mpeg1video,mpeg2video,mpeg4,msmpeg4v2,msmpeg4,png,prores,qtrle,rawvideo,tiff,wmv1,wmv2,xbm,zlib,aac,ac3,g722,g726,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,eac3,flac,mp2,opus,pcm_alaw,pcm_mulaw,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_s8,pcm_u16le,pcm_u32le,pcm_u8,sonic,truehd,tta,vorbis,wavpack,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,mov_text,srt,subrip,ttml,webvtt' --disable-decoders --enable-decoder='av1,dvvideo,flv,gif,h263,h264,hevc,mjpeg,mpeg1video,mpeg2video,mpegvideo,mpeg4,msmpeg4v2,msmpeg4v3,msvideo1,png,tscc,tscc2,vc1,vp8,vp9,webp,wmv1,wmv2,wmv3,zlib,aac,aac_latm,ac3,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,amrnb,amrwb,ape,dolby_e,eac3,flac,g722,g726,g729,m4a,mp3float,mp3,mp3adufloat,mp3adu,mp3on4,opus,pcm_alaw,pcm_mulaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,truehd,truespeech,vorbis,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,pgssub,mov_text,sami,srt,subrip,text,webvtt' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,ass,av1,avi,cavsvideo,codec2,concat,dash,dnxhd,eac3,flac,flv,g722,g726,g729,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mpegvideo,mv,mulaw,manifest,ogg,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,png_pipe,realtext,rm,rtp,rtsp,sami,sdp,srt,swf,vc1,wav,webm_dash,xmv' --cpu=armv8-a
     *   swresample  configuration: --target-os=android --prefix=ffmpeg-android/arm64-v8a --cross-prefix=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android- --enable-cross-compile --arch=aarch64 --cpu=armv8-a --cc=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --cxx=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang++ --nm=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm --strip=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --enable-cross-compile --sysroot=/Users/xufulong/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --enable-hwaccels --enable-static --disable-shared --disable-doc --enable-neon --enable-asm --disable-small --enable-jni --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-debug --enable-gpl --disable-avdevice --disable-indevs --disable-outdevs --extra-cflags='-Os -fPIC -march=armv8-a -mfpu=neon -mfloat-abi=softfp -Iffmpeg-android/arm64-v8a/include' --extra-ldflags='-lc -lm -ldl -llog -lgcc -lz -Lffmpeg-android/arm64-v8a/lib' --enable-avcodec --enable-avformat --enable-avutil --enable-swresample --enable-swscale --enable-avfilter --enable-network --enable-bsfs --disable-postproc --enable-filters --disable-encoders --enable-libmp3lame --enable-libx264 --enable-encoder='libmp3lame,libx264' --enable-encoder='apng,bmp,dvvideo,flv,gif,h263,h264,hdr,jpeg2000,ljpeg,mjpeg,mpeg1video,mpeg2video,mpeg4,msmpeg4v2,msmpeg4,png,prores,qtrle,rawvideo,tiff,wmv1,wmv2,xbm,zlib,aac,ac3,g722,g726,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,eac3,flac,mp2,opus,pcm_alaw,pcm_mulaw,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_s8,pcm_u16le,pcm_u32le,pcm_u8,sonic,truehd,tta,vorbis,wavpack,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,mov_text,srt,subrip,ttml,webvtt' --disable-decoders --enable-decoder='av1,dvvideo,flv,gif,h263,h264,hevc,mjpeg,mpeg1video,mpeg2video,mpegvideo,mpeg4,msmpeg4v2,msmpeg4v3,msvideo1,png,tscc,tscc2,vc1,vp8,vp9,webp,wmv1,wmv2,wmv3,zlib,aac,aac_latm,ac3,adpcm_ima_qt,adpcm_ima_wav,adpcm_ms,alac,amrnb,amrwb,ape,dolby_e,eac3,flac,g722,g726,g729,m4a,mp3float,mp3,mp3adufloat,mp3adu,mp3on4,opus,pcm_alaw,pcm_mulaw,pcm_dvd,pcm_f16le,pcm_f24le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,pcm_s16be,pcm_s16le,pcm_s24be,pcm_s24le,pcm_s32be,pcm_s32le,pcm_s64be,pcm_s64le,pcm_u16be,pcm_u16le,pcm_u24be,pcm_u24le,pcm_u32be,pcm_u32le,pcm_vidc,pcm_zork,truehd,truespeech,vorbis,wmav1,wmav2,ssa,ass,dvbsub,dvdsub,pgssub,mov_text,sami,srt,subrip,text,webvtt' --enable-muxers --enable-parsers --enable-nonfree --enable-protocols --enable-openssl --enable-protocol=https --disable-demuxers --enable-demuxer='aac,ac3,alaw,amr,amrnb,amrwb,ape,asf,asf_o,ass,av1,avi,cavsvideo,codec2,concat,dash,dnxhd,eac3,flac,flv,g722,g726,g729,gif,gif_pipe,h263,h264,hevc,hls,image2,image2pipe,jpeg_pipe,lrc,m4v,matroska,webm,mjpeg,mov,mp4,m4a,3gp,mp3,mpeg,mpegts,mpegvideo,mv,mulaw,manifest,ogg,pcm_s16be,pcm_s16le,pcm_s32be,pcm_s32le,pcm_f32be,pcm_f32le,pcm_f64be,pcm_f64le,png_pipe,realtext,rm,rtp,rtsp,sami,sdp,srt,swf,vc1,wav,webm_dash,xmv' --cpu=armv8-a
     *   libavutil      58.  2.100 / 58.  2.100
     *   libavcodec     60.  3.100 / 60.  3.100
     *   libavformat    60.  3.100 / 60.  3.100
     *   libavfilter     9.  3.100 /  9.  3.100
     *   libswscale      7.  1.100 /  7.  1.100
     *   libswresample   4. 10.100 /  4. 10.100
     *Input #0, flac, from '/storage/emulated/0/Download/netease/cloudmusic/Music/121/黄霄雲 - 天下无双 (Live).flac':
     *   Duration:
     * 00:04:39.89
     * , start:
     * 0.000000
     * , bitrate:
     * 975 kb/s
     *
     *   Stream #0:0
     * : Audio: flac, 48000 Hz, stereo, s16
     *
     * Stream mapping:
     *   Stream #0:0 -> #0:0
     *  (flac (native) -> mp3 (libmp3lame))
     *
     * Press [q] to stop, [?] for help
     * Output #0, mp3, to '/storage/emulated/0/Download/netease/cloudmusic/Music/121/黄霄雲 - 天下无双 (Live).mp3':
     *   Metadata:
     *     TSSE            :
     * Lavf60.3.100
     *
     *   Stream #0:0
     * : Audio: mp3, 48000 Hz, stereo, s16p
     *
     *     Metadata:
     *       encoder         :
     * Lavc60.3.100 libmp3lame
     *
     * size=       0kB time=-577014:32:22.77 bitrate=  -0.0kbits/s speed=N/A
     * size=     256kB time=00:00:17.97 bitrate= 116.7kbits/s speed=35.9x
     * size=     512kB time=00:00:38.66 bitrate= 108.5kbits/s speed=38.6x
     * size=     768kB time=00:00:58.68 bitrate= 107.2kbits/s speed=39.1x
     * size=    1024kB time=00:01:18.91 bitrate= 106.3kbits/s speed=39.4x
     * size=    1280kB time=00:01:37.84 bitrate= 107.2kbits/s speed=39.1x
     * size=    1792kB time=00:01:57.76 bitrate= 124.7kbits/s speed=39.2x
     * size=    2048kB time=00:02:17.78 bitrate= 121.8kbits/s speed=39.3x
     * size=    2304kB time=00:02:36.33 bitrate= 120.7kbits/s speed=  39x
     * size=    2560kB time=00:02:54.84 bitrate= 119.9kbits/s speed=38.8x
     * size=    2816kB time=00:03:14.23 bitrate= 118.8kbits/s speed=38.8x
     * size=    3328kB time=00:03:35.76 bitrate= 126.4kbits/s speed=39.2x
     * size=    3584kB time=00:03:55.12 bitrate= 124.9kbits/s speed=39.1x
     * size=    3840kB time=00:04:13.70 bitrate= 124.0kbits/s speed=  39x
     * size=    4096kB time=00:04:32.76 bitrate= 123.0kbits/s speed=38.9x
     * size=    4374kB time=00:04:39.88 bitrate= 128.0kbits/s speed=38.9x
     * video:0kB audio:4374kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:
     * 0.005269%
     *
     *
     * */
    private fun logSimple(){
        if(logBuf.isEmpty()) return
        Log.i("LogSimple()","running simple")

        logBuf= logBuf.filterNot { it.isBlank() }
        var outbuf=logBuf.subList(0,3).joinToString("\n")//获取前3行

        var pos= arrayOf(-1,-1,-1,-1,-1,-1,-1)

        logBuf.forEachIndexed{//初筛
            index,line->
            if(line.contains("libswresample",ignoreCase = true)){
                    pos[NONE.value]=index+1
            }
        }
        var logOne = logBuf.subList(pos[NONE.value],logBuf.size)//.joinToString("\n")

        if(logErr || !logSim) {
            outbuf+=logOne.joinToString("\n")

        }else {//二筛
            logOne= logOne.filterNot { it.isBlank() }
            pos[NONE.value]=-1
            logOne.forEachIndexed{
                    index,line->
                if(line.contains("Input")){
                    pos[INPUT.value] = index

                }
                else if(line.contains("Output") && pos[OUTPUT.value] < 0){
//                    outbuf += line+"\n"
                    pos[OUTPUT.value] = index

                }else if(line.contains("Stream #0")){
                    if(pos[OUTPUT.value]>=0) {
                        pos[STREAM_OUT.value] = index

                    }else if(pos[STREAM_IN.value] < 0){//Output前面有两个Stream，不判断会进来两次
                        pos[STREAM_IN.value] = index
                    }
                }else if(line.contains("Stream mapping")){
                    pos[MAPPING.value] = index
                }else if(line.contains("Metadata")) {
                    if(pos[STREAM_OUT.value] >=0 ) pos[NONE.value]=index
                }else if(line.contains("size") && pos[SIZE.value]<0){
                    if(logOne[index+1].contains("audio:")) {
                        pos[SIZE.value] = index
                    }
//                        outbuf += line+"\n"
                }
            }
            for( index in POSNAME.values() ) {//检查标志是否完整
                if (pos[index.value] < 0) {
                    Log.w("logSimple()", "logsimple can't find keyword \"${index.name}\",put all log")
                    outbuf += logOne.joinToString("\n")
                    pos[1] =-1
                }
            }
            if(pos[1] >=0){//根据标记位置按顺序合成简化代码
                outbuf += logOne.subList(pos[INPUT.value], pos[INPUT.value]+1).joinToString("\n")
                outbuf += logOne.subList(pos[INPUT.value]+1, pos[STREAM_IN.value]).joinToString(" ")+"\n"//获取input数据

                outbuf += logOne.subList(pos[STREAM_IN.value], pos[MAPPING.value]).joinToString("")+"\n"
                outbuf += logOne.subList(pos[MAPPING.value], pos[OUTPUT.value]-1).joinToString("\n")+"\n"//获取Stream信息

                outbuf += logOne.subList(pos[OUTPUT.value], pos[OUTPUT.value]+1).joinToString("\n") //获取output数据

                outbuf += logOne.subList(pos[STREAM_OUT.value], pos[NONE.value]).joinToString(" ")+"\n" //获取output Stream数据

                outbuf += logOne.subList(pos[SIZE.value],pos[SIZE.value]+1).joinToString("\n")
                outbuf += logOne.subList(pos[SIZE.value]+1,pos[SIZE.value]+3).joinToString(" ")+"\n" //获取size数据

            }
        }
        Log.i("logSimple()",outbuf)
//        if(logErr) Agent.log(false,outbuf)
         Agent.newLog(outbuf)
    }

    private fun run():Int{
        if (this.input.isEmpty() || this.output.isEmpty()) {
            Log.w("FFMPEG()","parameter error,need init right path")
            return result
        }
        result =0
        if(input.isEmpty() or output.isEmpty()) {
            Log.e("FFmpeg() run:","no input or output")
            Agent.newLog("<Err>FFmpeg() run:\",\"no input or output")
            return -1
        }
        commandLsit = arrayOf("ffmpeg")
        commandLsit +=arrayOf("-i")
        commandLsit +=arrayOf(input)
        commandLsit +=arrayOf(output)

        if(!(timeStart.isNullOrEmpty() || timeEnd.isNullOrEmpty() || timeStart == timeEnd)){
            if(sampling) {//-c copy 表示无损剪辑，不重采样
                commandLsit += arrayOf("-c")
                commandLsit += arrayOf("copy")
            }
            commandLsit +=arrayOf("-ss")
            commandLsit += arrayOf(timeStart?:"")
            commandLsit +=arrayOf("-to")
            commandLsit +=arrayOf(timeEnd?:"")
        }
        val command= commandLsit.joinToString(" ")


        try {
            Log.i("class ffmpeg() run", command)

            result = Agent.ffmpeg(commandLsit)//command.split("\\s+").toTypedArray())
            Log.i("class ffmpeg()", "result:${result}")
            logSimple()
            callback?.let { it(result) }

        } catch (e: Exception) {
            result = -1
            Log.e("class ffmpeg()", e.toString())
            logErr = true
            Agent.newLog("<Err>${e}")
            callback?.let { it(result) }
        }

        return result
    }
    private fun stop(){
        if(result>0)Agent.FcancelTask(result)
    }

    /**必须调用此函数更新参数才能使用ffmpeg()功能*/
    fun init(input:String?,output:String?,timeS:String?, timeE:String?,executor: ExecutorService?,callback: ((Int) -> Unit)?
    ){
        logBuf= listOf("")
        if(input != null)this.input = input
        if(output != null)this.output = output
        if(timeS != null)this.timeStart = timeS
        if(timeE != null)this.timeEnd = timeE
        /**如果传递executor参数则更新，如果不传递则使用原值或既定值 */
        if(executor !=null) this.executor =executor
//        if(timeS != null && timeE != null)  sampling = true
        else if(this.executor == null) this.executor =Agent.executorService //既定值

        if(callback != null) this.callback = callback
        logErr = false

    }
    fun clear(){
        this.input = ""
        this.output = ""
        this.timeStart = null
        this.timeEnd = null
        this.executor = null
        this.callback = null
        this.sampling = false
        this.result = 0
        this.logErr = false
        this.logBuf = listOf("")
        this.callback = null
        this.logSim = true
    }
    private fun cmd(command:String){
        result=Agent.ffmpeg(command.split("\\s+").toTypedArray())//用法错误,要传递正确数组，这个方法会拆开含有空格的文件名
    }
    companion object{
        private val handler=FFMPEG()
        fun get():FFMPEG{
            return handler
        }
        fun init(
            input:String?=null,
            output:String?=null,
            timeS: String?=null, timeE: String?=null,
            sampling:Boolean?=null,
            executor: ExecutorService?=null,
            callback: ((Int) -> Unit)?=null){
            handler.sampling=sampling?:false
           return handler.init(input,output,timeS,timeE,executor,callback)
        }
        /*fun cmd(command: String){
            handler.cmd(command)
        }*/
        fun run():Int{
            return handler.run()
        }
    }

}



/**
 * 实现方法
* https://blog.csdn.net/baidu_30251831/article/details/121346510
 *
 * 正在使用
 * https://blog.csdn.net/ice_bear221/article/details/128556581
 *
* */